﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Xioa_UI.Controls;


[TemplatePart(Name = "Xioa_Box", Type = typeof(TextBox))]
public class XioaTextBox : TextBox
{
    private TextBox? _textBox;
    public XioaTextBox()
    {
        //LostFocus += FocusDependencyEvent; //失去焦点
        //GotFocus += GetFocusDependencyEvent;// 获取焦点
        //TextChanged += XioaTextBox_TextChanged;


    }

    public override void OnApplyTemplate()
    {
        if (_textBox != null)
            _textBox = null;

        _textBox = this.GetTemplateChild("Xioa_Box") as TextBox;

        if (_textBox != null)
        {
            _textBox.TextChanged += _textBox_Changed;
        }
    }
    private void _textBox_Changed(object sender, TextChangedEventArgs e)
    {
        var data = (TextBox)sender;
        this.Text = data.Text; 
        ShowPlaceholder = Text.Length > 0 ? Visibility.Hidden : Visibility.Visible;
        //this.Text = _textBox!.Text;
    }
    private void GetFocusDependencyEvent(object sender, RoutedEventArgs e)
    {
        ShowPlaceholder = Visibility.Hidden;
    }

    private void XioaTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        ShowPlaceholder = Text.Length > 0 ? Visibility.Hidden : Visibility.Visible;
    }

    private void FocusDependencyEvent(object? sender, RoutedEventArgs? e)
    {
        ShowPlaceholder = Text.Length > 0 ? Visibility.Hidden : Visibility.Visible;
    }



    public static readonly DependencyProperty PlaceholderProperty =
        ElementBase.Property<XioaTextBox, string>("PlaceholderProperty", "");

    public static readonly DependencyProperty PlaceholderFontSizeProperty =
        ElementBase.Property<XioaTextBox, double>("PlaceholderFontSizeProperty", 10);

    public static readonly DependencyProperty TitleProperty =
        ElementBase.Property<XioaTextBox, string>("TitleProperty", "");

    public static readonly DependencyProperty TitleMinWidthProperty =
        ElementBase.Property<XioaTextBox, double>("TitleMinWidthProperty", 100);

    public static readonly DependencyProperty TitleForegroundProperty =
        ElementBase.Property<XioaTextBox, Brush>("TitleForegroundProperty");

    public static readonly DependencyProperty TitleFontSizeProperty =
        ElementBase.Property<XioaTextBox, double>("TitleFontSizeProperty", 10);

    public static readonly DependencyProperty ShowPlaceholderProperty =
        ElementBase.Property<XioaTextBox, Visibility>("ShowPlaceholderProperty", Visibility.Visible);


    public Visibility ShowPlaceholder
    {
        get => (Visibility)GetValue(ShowPlaceholderProperty);
        set => SetValue(ShowPlaceholderProperty, value);
    }

    public double PlaceholderFontSize
    {
        get => (double)GetValue(PlaceholderFontSizeProperty);
        set => SetValue(PlaceholderFontSizeProperty, value);
    }
    public string Placeholder
    {
        get => (string)GetValue(PlaceholderProperty);
        set => SetValue(PlaceholderProperty, value);
    }
    public string Title
    {
        get => (string)GetValue(TitleProperty);
        set => SetValue(TitleProperty, value);
    }

    public double TitleMinWidth
    {
        get => (double)GetValue(TitleMinWidthProperty);
        set => SetValue(TitleMinWidthProperty, value);
    }

    public double TitleFontSize
    {
        get => (double)GetValue(TitleFontSizeProperty);
        set => SetValue(TitleFontSizeProperty, value);
    }

    public Brush TitleForeground
    {
        get => (Brush)GetValue(TitleForegroundProperty);
        set => SetValue(TitleForegroundProperty, value);
    }
}